﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace starLiGHT.GUI.Widgets
{
    public enum Orientation
    {
        Vertical = 0,
        Horizontal = 1,
    }

    public class StackPanel : ContainerWidget
    {
        private float spacing;
        private Orientation orientation;

        List<Widget> widgetOrder;

        public StackPanel(GuiManager manager, string name)
            : base(manager, name)
        {
            Init();
        }
        public StackPanel(GuiManager manager, string name, string layout)
            : base(manager, name, layout)
        {
            Init();
        }

        private void Init()
        {
            widgetOrder = new List<Widget>();
            Widgets.WidgetAdded += WidgetAdded;
            Widgets.WidgetRemoved += WidgetRemoved;
        }

        public float Spacing
        {
            get
            {
                return spacing;
            }
            set
            {
                if (spacing != value)
                {
                    spacing = value;
                    ReAlignChildren();
                }
            }
        }
        public Orientation Orientation
        {
            get
            {
                return orientation;
            }
            set
            {
                if (orientation != value)
                {
                    orientation = value;
                    ReAlignChildren();
                }
            }
        }

        void WidgetAdded(object sender, EventArgs e)
        {
            Widget w = sender as Widget;
            
            if(!widgetOrder.Contains(w))
                widgetOrder.Add(w);

            w.SizeChanged += WidgetChanged;

            ReAlignChildren();
        }
        void WidgetRemoved(object sender, EventArgs e)
        {
            (sender as Widget).SizeChanged -= WidgetChanged;
            ReAlignChildren();
        }

        void WidgetChanged(object sender, EventArgs e)
        {
            ReAlignChildren();
        }

        private void ReAlignChildren()
        {
            float size = 0;

            foreach (Widget w in widgetOrder)
            {
                if (!Widgets.Contains(w))
                    continue;

                w.Location = (Orientation == GUI.Widgets.Orientation.Vertical)
                            ? new Vector2(0, size)
                            : new Vector2(size, 0);

                float widgetSize = (Orientation == GUI.Widgets.Orientation.Vertical)
                            ? w.Size.Y
                            : w.Size.X;

                size += widgetSize + spacing;
            }

            switch (Orientation)
            {
                case GUI.Widgets.Orientation.Vertical:
                    this.Size = new Vector2(this.Size.X, size);
                    break;
                case GUI.Widgets.Orientation.Horizontal:
                    this.Size = new Vector2(size, this.Size.Y);
                    break;
            }
        }
        public override void Draw(GameTime gameTime, IGuiRenderer renderer)
        {
            base.Draw(gameTime, renderer);
        }

    }
}
